﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using SQLServerHealthCheck.Model;
using System.Windows.Media;
using SQLServerHealthCheck.SQLServer.AutoFixes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System;
using SQLServerOptimizer.PL.WPF;
using System.Windows.Threading;
using System.Management;
using System.Windows.Controls.DataVisualization.Charting;


namespace PlayGround.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<IssueType> issuestypes { get; set; }
        HealthCheckDataContext dc;
       // ObservableCollection<KeyValuePair<DateTime, int>> cpuUsage;
        ObservableCollection<KeyValuePair<string, int>> cpuUsage2;

        int collum;
        int row;
        public MainWindow()
        {

            InitializeComponent();
        //    cpuUsage = new ObservableCollection<KeyValuePair<string, int>>();
            cpuUsage2 = new ObservableCollection<KeyValuePair<string, int>>();

            //chart1.DataContext = cpuUsage;

            chart2.DataContext = cpuUsage2;
            chart3.DataContext = cpuUsage2;
            chart4.DataContext = cpuUsage2;
            chart5.DataContext = cpuUsage2;
            chart6.DataContext = cpuUsage2;
            fillGraphs();
            fillPLEandConnections();
            fillDiskIO();
        }

        void fillGraphs()
        {

            dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);

            var servers = from a in dc.PerfServers
                          where a.PerfServerID == 1
                          select a;
            foreach (var server in servers)
            {



                var cpus = from a in dc.CpuCores
                           where a.PerfServer == server
                           select a;

                foreach (var cpu in cpus)
                {
                    ObservableCollection<KeyValuePair<DateTime, int>> cpuUsage = new ObservableCollection<KeyValuePair<DateTime, int>>();
                    var usage = from a in dc.CpuUsages
                                where a.CpuCore == cpu
                                select a;
                    foreach (var item in usage)
                    {


                        cpuUsage.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(item.Time.Time1), Convert.ToInt32(item.Percentage)));

                    }

                    LineSeries new1 = new LineSeries();
                    new1.ItemsSource = cpuUsage;
                    new1.Title = cpu.Name;
                    // new1.Style = StaticResourceExtension.                    //      new1.DataPointStyle.A //.Setters.Add(new Setter(LineDataPoint.VisibilityProperty,Visibility.Collapsed));
                    new1.DependentValuePath = "Value";
                    new1.IndependentValuePath = "Key";
                    //   Style style = new Style(typeof(LineDataPoint));

                    //  style.Setters.Add(new Setter(LineDataPoint.WidthProperty, 0.0));
                    //   style.Setters.Add(new Setter(LineDataPoint.HeightProperty, 0.0));

                    //     new1.OverridesDefaultStyle = false;
                    //style.Setters.Add(new Setter(LineDataPoint.TemplateProperty, null));
                    //     new1.DataPointStyle = style;
                    chart1.Series.Add(new1);

                }


            }
            fillPLEandConnections();
            fillDiskIO();
        }

        void fillPLEandConnections()
        {
            ObservableCollection<KeyValuePair<DateTime, int>> ple = new ObservableCollection<KeyValuePair<DateTime, int>>();
            ObservableCollection<KeyValuePair<DateTime, int>> connections = new ObservableCollection<KeyValuePair<DateTime, int>>();
            var servers = from a in dc.PerfServers
                          where a.PerfServerID == 1
                          select a;
            foreach (var server in servers)
            {


                var instances = from a in dc.Instances
                                where a.InstanceID == 1
                                select a;
                foreach (var item in instances)
                {


                    var instancedata = from a in dc.InstanceDatas
                                       where a.Instance == item
                                       select a;
                    foreach (var instancedataitem in instancedata)
                    {
                        ple.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(instancedataitem.Time.Time1), Convert.ToInt32(instancedataitem.pageLifeExpectancy)));
                        connections.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(instancedataitem.Time.Time1), Convert.ToInt32(instancedataitem.connections)));
                    }
                    LineSeries new1 = new LineSeries();
                    new1.ItemsSource = ple;
                    new1.Title = item.Name;
                    new1.DependentValuePath = "Value";
                    new1.IndependentValuePath = "Key";
                    chart2.Series.Add(new1);
                    LineSeries new2 = new LineSeries();
                    new2.ItemsSource = connections;
                    new2.Title = item.Name;
                    new2.DependentValuePath = "Value";
                    new2.IndependentValuePath = "Key";
                    chart3.Series.Add(new2);
                }
            }

        }

        void fillDiskIO()
        {

            ObservableCollection<KeyValuePair<DateTime, int>> diskWrite = new ObservableCollection<KeyValuePair<DateTime, int>>();
            ObservableCollection<KeyValuePair<DateTime, int>> diskRead = new ObservableCollection<KeyValuePair<DateTime, int>>();
            ObservableCollection<KeyValuePair<DateTime, int>> diskLatencyWrite = new ObservableCollection<KeyValuePair<DateTime, int>>();
            ObservableCollection<KeyValuePair<DateTime, int>> diskLatencyRead = new ObservableCollection<KeyValuePair<DateTime, int>>();

            var servers = from a in dc.PerfServers
                          where a.PerfServerID == 1
                          select a;
            foreach (var server in servers)
            {
                var disks = from a in dc.Disks
                            where a.PerfServer == server
                            select a;
                foreach (var disk in disks)
                {
                    var diskios = from a in dc.DiskIOs
                                  where a.Disk == disk
                                  select a;

                    foreach (var diskio in diskios)
                    {
                        diskWrite.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(diskio.Time.Time1), Convert.ToInt32(diskio.AvgDiskSecPerWrite)));
                        diskLatencyWrite.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(diskio.Time.Time1), Convert.ToInt32(diskio.DiskWritesPerSec)));
                        diskRead.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(diskio.Time.Time1), Convert.ToInt32(diskio.AvgDiskSecPerRead)));

                        diskLatencyRead.Add(new KeyValuePair<DateTime, int>(Convert.ToDateTime(diskio.Time.Time1), Convert.ToInt32(diskio.DiskReadsPerSec)));


                    }
                    LineSeries new1 = new LineSeries();
                    new1.ItemsSource = diskWrite;
                    new1.Title = "" + disk.name + "Write";
                    new1.DependentValuePath = "Value";
                    new1.IndependentValuePath = "Key";
                    chart4.Series.Add(new1);
                    LineSeries new4 = new LineSeries();
                    new4.ItemsSource = diskRead;
                    new4.Title = "" + disk.name + "Read";
                    new4.DependentValuePath = "Value";
                    new4.IndependentValuePath = "Key";
                    chart4.Series.Add(new4);
                    LineSeries new2 = new LineSeries();
                    new2.ItemsSource = diskLatencyWrite;
                    new2.Title = "" + disk.name + "Write";
                    new2.DependentValuePath = "Value";
                    new2.IndependentValuePath = "Key";
                    chart5.Series.Add(new2);


                    LineSeries new5 = new LineSeries();
                    new5.ItemsSource = diskLatencyRead;
                    new5.Title = "" + disk.name + "Read";
                    new5.DependentValuePath = "Value";
                    new5.IndependentValuePath = "Key";
                    chart5.Series.Add(new5);

                }

            }


        }

    
    }

}
